import request from '@/utils/request'
import { setTokenInfo, removeTokenInfo } from '@/utils/storage'
/**
 * 获取验证码
 * @param {*} mobile
 * @returns
 */
export const sendCode = (mobile) => {
  return async (dispatch) => {
    const res = await request({
      url: `/sms/codes/${mobile}`,
      method: 'get',
    })
    console.log(res)
  }
}

const saveToken = (payload) => {
  return {
    type: 'login/token',
    payload,
  }
}

/**
 * 登录
 * @param {*} params
 * @returns
 */
export const login = (params) => {
  return async (dispatch) => {
    const res = await request.post('/authorizations', params)
    const tokenInfo = res.data
    console.log(tokenInfo)
    // 保存token到redux中
    dispatch(saveToken(tokenInfo))
    // 保存到本地
    setTokenInfo(res.data)
  }
}

/**
 * 退出
 * @returns
 */
export const logout = () => {
  return (dispatch) => {
    removeTokenInfo()
    dispatch({
      type: 'login/logout',
    })
  }
}
